//
//  ErandDudaoListByDayViewController.m
//  KIASupervisory
//
//  Created by tan_xin on 14-7-10.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "ErandDudaoListByDayViewController.h"
#import "LStoreProblemListViewController.h"

@interface ErandDudaoListByDayViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dudaoList;


@end

@implementation ErandDudaoListByDayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dudaoList = [NSMutableArray array];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.showLeftBtn = YES;
    self.showRightBtn = NO;
    self.title = @"当日出差督导";
    
    [super viewWillAppear:animated];
    
    [self performSelector:@selector(getTodayErandDudaoList) withObject:nil afterDelay:0.3];
}

- (void)refreshViewByReloadData {
    [self getTodayErandDudaoList];
}

- (void)leftNavItemAction:(id)sender {
    AppDelegate *dele = [AppDelegate sharedDelegate];
    DDMenuController *menuVC = dele.ddLeftMenu;
    [menuVC showLeft:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//获取当日出差的督导列表
- (void)getTodayErandDudaoList {
    if (!self.isRefreshing) {
        self.isRefreshing = YES;
        UserInfo *user = [UserInfo instance];
        NSString *gonghao = [user.info objectForKeyOrNil:@"yhm"];
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                               gonghao,@"gh", nil];
        [self.networkManager requestWithMethod:selectUserinfoByLeaderGh parameter:param delegate:self];
        [self.HUD show:YES];
    }
}

#pragma mark - NetworkManagerDelegate
- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    [self endRefresh];
    self.isRefreshing = NO;
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        int code = [[data objectForKeyOrNil:@"code"] integerValue];
        if (code == 0) {
            [self.dudaoList removeAllObjects];
            
            if ([data count] > 0) {
                id dataObject = [data objectForKeyOrNil:@"data"];
                if ([dataObject isKindOfClass:[NSArray class]]) {
                    [self.dudaoList addObjectsFromArray:dataObject];
                }
            }
            
            [self.tableView reloadData];
        } else {
            NSString *msg = [data objectForKeyOrNil:@"msg"];
            if ([msg  length] > 0) {
//                msg = @"当日出差督导列表获取失败，请稍后重试";
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:msg
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
                [alert show];
            }
            
        }
    }
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    [self.HUD hide:YES];
    [self endRefresh];
    self.isRefreshing = NO;
    
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    NSInteger code = error.code;
    if (code != 1) {
        if ([msg length] == 0) {
            msg = @"当日出差督导列表获取失败，请稍后重试";
        }
    }
    
    if ([msg length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}


#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    [self endRefresh];
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dudaoList count];
//    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"ErandDudaoListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    UILabel *userNameLbl = (UILabel *)[cell.contentView viewWithTag:101];
    UILabel *userIDLbl = (UILabel *)[cell.contentView viewWithTag:102];
    UILabel *dateLbl = (UILabel *)[cell.contentView viewWithTag:103];
    UILabel *jxsNameLbl = (UILabel *)[cell.contentView viewWithTag:104];
    UILabel *jxsCodeLbl = (UILabel *)[cell.contentView viewWithTag:105];
    UILabel *reasonLbl = (UILabel *)[cell.contentView viewWithTag:106];
    UIButton *statusBtn = (UIButton *)[cell.contentView viewWithTag:107];
    
    NSInteger row = indexPath.row;
    if ([self.dudaoList count] > row) {
        NSDictionary *cellData = [self.dudaoList objectAtIndex:row];
        NSString *name = [cellData objectForKeyOrNil:@"xm"];
        NSString *gh = [cellData objectForKeyOrNil:@"gh"];
        NSString *jxsName = [cellData objectForKeyOrNil:@"jxsmc"];
        NSString *jxsCode = [cellData objectForKeyOrNil:@"jxsdm"];
        NSString *reason = [cellData objectForKeyOrNil:@"ccmd"];
        NSString *erandTime = [cellData objectForKeyOrNil:@"ccsj"]; //出差时间
        BOOL finished = [[cellData objectForKeyOrNil:@"sfwc"] boolValue]; //是否完成
        
        userNameLbl.text = name;
        userIDLbl.text = gh;
        dateLbl.text = erandTime;
        jxsNameLbl.text = jxsName;
        jxsCodeLbl.text = jxsCode;
        reasonLbl.text = reason;
        
        if (finished) {
            [statusBtn setTitle:@"完成" forState:UIControlStateNormal];
            [statusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [statusBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [statusBtn setBackgroundImage:[UIImage imageNamed:@"tip_通过.png"] forState:UIControlStateNormal];
        } else {
            [statusBtn setTitle:@"未完成" forState:UIControlStateNormal];
            [statusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [statusBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [statusBtn setBackgroundImage:[UIImage imageNamed:@"tip_未通过.png"] forState:UIControlStateNormal];
        }
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Leader" bundle:nil];
    LStoreProblemListViewController *storeProblemVC = (LStoreProblemListViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LeaderStoreProblemList"];
    storeProblemVC.viewType = EnterFromDRDD; //标识从当日出差督导界面进入
    NSInteger row = indexPath.row;
    if ([self.dudaoList count] > row) {
        NSDictionary *cellData = [self.dudaoList objectAtIndex:row];
        storeProblemVC.erandInfo = cellData;
    }
    [self.navigationController pushViewController:storeProblemVC animated:YES];
}

@end
